package com.example.yangxu0703.thundermapclient.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

import com.autonavi.rtbt.IFrameForRTBT;

import java.io.File;
import java.io.IOException;

import static com.example.yangxu0703.thundermapclient.bluetooth.AbsBluetooth.DISCONNECT;
import static com.example.yangxu0703.thundermapclient.bluetooth.AbsBluetooth.uuid;

public class ConnectThread extends Thread {

    private BluetoothDevice device;
    private BluetoothAdapter adapter;
    private BluetoothSocket socket;
    private AbsBluetooth.Listerner listerner;
    private SendMsg sendMsg;
    private String msg;
    private File file;
    private int mode = 3;
    private boolean issend=false;

    public ConnectThread(BluetoothAdapter adapter, BluetoothDevice device) {
        this.adapter = adapter;
        this.device = device;
        try {
            socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
            Log.i("CON_STATE", socket.isConnected() + "scoket");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setFile(File file) {
        this.file = file;
        mode = 1;
        issend=true;
    }

    public void setMsg(String msg) {
        this.msg = msg;
        mode = 0;
        issend=true;
    }


    @Override
    public void run() {
        super.run();
        adapter.cancelDiscovery();
        //开始连接判断(Bluetooth的状态)
        while (true) {
            if (issend) {
                  Log.i("CON_STATE", "start send!");
                 if (socket ==null){
                     continue;
                 }
                if (socket != null && !socket.isConnected()) {
                    try {
                        socket.connect();
                    } catch (IOException e) {
                        closeconnection();
                        e.printStackTrace();
                    }
                    Log.i("CON_STATE", "-------"+socket.isConnected());
                    if (socket.isConnected()) {
                        sendMsg = new SendMsg(socket);
                        if (mode == 0) {
                            sendMsg.setMsg(msg);
                        } else {
                            sendMsg.setFileSend(file);
                        }
                        sendMsg.start();
                        issend=false;
                    }
                    Log.i("CON_STATE", "Successed!" + socket.isConnected() + "scoket" + socket);
                } else {
                    sendMsg = new SendMsg(socket);
                    if (mode == 0) {
                        sendMsg.setMsg(msg);
                    } else {
                        sendMsg.setFileSend(file);
                    }
                    sendMsg.start();
                    issend=false;

                }
                sendMsg.setListerner(new AbsBluetooth.Listerner() {
                    @Override
                    public void observerConnection(int status) {
                        listerner.observerConnection(status);
                    }

                    @Override
                    public void backMessage(String msg) {
                        listerner.backMessage(msg);
                    }
                });
            }
        }
    }

    public void closeconnection() {
        try {
            Log.i("CON_STATE", "Failed!");
            socket.close();
            socket = null;
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        listerner.observerConnection(DISCONNECT);
    }

    public void setListerner(AbsBluetooth.Listerner listerner) {
        this.listerner = listerner;
    }


}